package m.store.service;

import m.base.model.StoreInfo;
import m.base.util.PowerUtil;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.store.model.StoreNumber;
import m.system.exception.MException;
import m.system.util.DateUtil;
import m.system.util.GenerateID;

import java.util.Date;

public class StoreNumberService extends Service {
    public static String getNextStockNumber() throws Exception {
        return nextNumber(PowerUtil.getSessionStoreOid(),StoreNumber.Type_Stock);
    }
    public static String getNextSaleNumber() throws Exception {
        return nextNumber(PowerUtil.getSessionStoreOid(),StoreNumber.Type_Sale);
    }
    public static String getNextPayNumber() throws Exception {
        return nextNumber(PowerUtil.getSessionStoreOid(),StoreNumber.Type_Pay);
    }
    public static String getNextIncomeNumber() throws Exception {
        return nextNumber(PowerUtil.getSessionStoreOid(),StoreNumber.Type_Income);
    }
    public static String getNextProduceNumber() throws Exception {
        return nextNumber(PowerUtil.getSessionStoreOid(),StoreNumber.Type_Produce);
    }
    public static String getNextCountNumber() throws Exception {
        return nextNumber(PowerUtil.getSessionStoreOid(),StoreNumber.Type_Count);
    }
    public static String getNextBeginNumber() throws Exception {
        return nextNumber(PowerUtil.getSessionStoreOid(),StoreNumber.Type_Begin);
    }
    public static String getNextStockNumber(String storeOid) throws MException {
        return nextNumber(storeOid,StoreNumber.Type_Stock);
    }
    public static String getNextSaleNumber(String storeOid) throws MException {
        return nextNumber(storeOid,StoreNumber.Type_Sale);
    }
    public static String getNextPayNumber(String storeOid) throws MException {
        return nextNumber(storeOid,StoreNumber.Type_Pay);
    }
    public static String getNextIncomeNumber(String storeOid) throws MException {
        return nextNumber(storeOid,StoreNumber.Type_Income);
    }
    public static String getNextProduceNumber(String storeOid) throws MException {
        return nextNumber(storeOid,StoreNumber.Type_Produce);
    }
    public static String getNextCountNumber(String storeOid) throws MException {
        return nextNumber(storeOid,StoreNumber.Type_Count);
    }
    public static String getNextBeginNumber(String storeOid) throws MException {
        return nextNumber(storeOid,StoreNumber.Type_Begin);
    }
    private static String fillNumber(int n){
        if(n<10) return "000"+n;
        else if(n<100) return "00"+n;
        else if(n<1000) return "0"+n;
        else return ""+n;
    }
    private static synchronized String nextNumber(String storeOid,String type) throws MException {
        try {
            String yearMonth=DateUtil.format(new Date(), "yyyyMM");
            StoreNumber model = ModelQueryList.getModel(StoreNumber.class, new String[]{"*"},
                QueryCondition.and(
                    QueryCondition.eq("store.oid", storeOid),
                    QueryCondition.eq("type", type),
                    QueryCondition.eq("yearMonth", yearMonth)
                )
            );
            if (null != model) {
                model.setMaxNumber(model.getMaxNumber()+1);
                ModelUpdateUtil.updateModel(model,"maxNumber");
            }else{
                model=new StoreNumber();
                model.setOid(GenerateID.generatePrimaryKey());
                model.setStore(new StoreInfo());
                model.getStore().setOid(storeOid);
                model.setType(type);
                model.setYearMonth(yearMonth);
                model.setMaxNumber(1);
                ModelUpdateUtil.insertModels(model);
            }
            return type+yearMonth+fillNumber(model.getMaxNumber());
        }catch (Exception e){
            throw new MException(StoreNumberService.class,"生成编号失败");
        }
    }
}
